#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int integerReplacement(int n) {
        long long ll = n;
        int ret = 0;
        while (ll != 1)
        {
            if (ll == 3) return ret + 2;
            if (ll % 2 == 0) ll /= 2;
            else
            {
                if ((ll + 1) % 4 == 0) ll++;
                else ll--;
            }
            ret++;
        }
        return ret;
    }
};